package com.ymatou.iapi.godmch.testcase;

import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymatou.iapi.godmch.parameter.CreateCodeBean;
import com.ymatou.iapi.godmch.parameter.createmchBean;
import com.ymatou.iapi.godmch.parameter.editmchBean;
import com.ymatou.iapi.godmch.service.editmchCall;
import com.ymatou.mq.admin.parameter.MessageListBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.business.service.MqQueryCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.mch_info;
import com.ymttest.database.model.mch_relation;
import com.ymttest.database.sqlwapper.godmch.DefGodMchWapper;
import com.ymttest.utils.TestDataManager;
/**
*设置商户信息(头像,名称,微信号)
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_editmch{
	private static editmchBean bean;
	private static editmchCall call;
	DefGodMchWapper DefGodMchWapper;
	private MqQueryCallService mqQueryCallService=new MqQueryCallService();
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("设置商户信息(头像,名称,微信号)");
	}

	@Before
	public void caseUp() {
		bean=new editmchBean();
		call=new editmchCall();
		DefGodMchWapper=new DefGodMchWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_editmch_001() {
		Logger.start(true,"设置商户信息(头像,名称,微信号),验证商户是代理商,返回成功");
		try {
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			String code = GodmchManageCallService.createCodeCall(ccbean).getCode();
			Integer agentid = GodmchManageCallService.CreateSellerMch(code);
			bean.setAvatar("http://pic1.ymatou.com/G01/M00/B3/61/rBBlD10e-MqAPEwqAAlJOe_n_4U923_400_533_n_w_o.jpg");
			bean.setMchId(agentid);
			bean.setMchTitle("updateTitle");
			bean.setWechat("uwechat");
			call.setData(bean);
			call.callService();			
			Logger.verifyEquals("200", call.getString("status"), "验证status");
			 mch_info dbmr = DefGodMchWapper.selectMchInfo(agentid).get(0);
			Logger.verifyEquals(bean.getAvatar(), dbmr.getAvatar(), "验证Avatar");
			Logger.verifyEquals(bean.getMchTitle(), dbmr.getMchTitle(), "验证MchTitle");
			Logger.verifyEquals(bean.getWechat(), dbmr.getWechat(), "验证Wechat");
			MessageListBean MessageListBean = MqQueryCallService.initGodmchManage("change_avatar-"+agentid,"change_avatar");
			List <Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
			if(joa.size()==0){
				Thread.sleep(5000);
				 joa = MqQueryCallService.GetMqMessage(MessageListBean);
			}
			joa.forEach(ele->{
				Logger.debug(ele.toString());
			});
			Logger.verifyEquals(agentid.toString(), joa.get(0).get("mchId").toString(), "验证总线消息mchId");
			
	        
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_editmch_002() {
		Logger.start(true,"设置商户信息(头像,名称,微信号),验证商户是渠道商,返回成功");
		try {
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			bean.setAvatar("http://www.ymatou.com/new.jpg");
			bean.setMchId(ccbean.getMchId());
			bean.setMchTitle("updateTitle");
			bean.setWechat("uwechat");
			call.setData(bean);
			call.callService();			
			Logger.verifyEquals("200", call.getString("status"), "验证status");
			 mch_info dbmr = DefGodMchWapper.selectMchInfo(ccbean.getMchId()).get(0);
			Logger.verifyEquals(bean.getAvatar(), dbmr.getAvatar(), "验证Avatar");
			Logger.verifyEquals(bean.getMchTitle(), dbmr.getMchTitle(), "验证MchTitle");
			Logger.verifyEquals(bean.getWechat(), dbmr.getWechat(), "验证Wechat");
			//mq验证耗时 只验证第一个就可以了
//			MessageListBean MessageListBean = MqQueryCallService.initGodmchManage("change_avatar-"+bean.getMchId(),"change_avatar");
//			List <Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
//			if(joa.size()==0){
//				Thread.sleep(5000);
//				 joa = MqQueryCallService.GetMqMessage(MessageListBean);
//			}
//			joa.forEach(ele->{
//				Logger.debug(ele.toString());
//			});
//			Logger.verifyEquals(bean.getMchId().toString(), joa.get(0).get("mchId").toString(), "验证总线消息mchId");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_editmch_003() {
		Logger.start(true,"设置商户信息(头像,名称,微信号),验证商户是客服,返回成功");
		try {
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			UserCreateBean usercreateBean=DGUserCallService.initCsUsercreateBean();
			 int csId=DGUserCallService.UserCreateCall(usercreateBean);
			 GodmchManageCallService.CreateCSCall(ccbean.getMchId(), csId, 1);
			bean.setAvatar("http://www.ymatou.com/new.jpg");
			bean.setMchId(csId);
			bean.setMchTitle("updateTitle");
			bean.setWechat("uwechat");
			call.setData(bean);
			call.callService();			
			Logger.verifyEquals("200", call.getString("status"), "验证status");
			Logger.verifyEquals(true, DefGodMchWapper.selectMchInfo(csId).isEmpty(),"验证没有记录");
			//客服没有商家信息
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_editmch_004() {
		Logger.start(true,"设置商户信息(头像,名称,微信号),验证商户名称含有emoji,返回成功");
		try {
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			String code = GodmchManageCallService.createCodeCall(ccbean).getCode();
			Integer agentid = GodmchManageCallService.CreateSellerMch(code);
			bean.setAvatar("http://www.ymatou.com/new.jpg");
			bean.setMchId(agentid);
			bean.setMchTitle("updateTitle😀");
			bean.setWechat("uwechat");
			call.setData(bean);
			call.callService();			
			Logger.verifyEquals("5000", call.getString("status"), "验证status");
//			 mch_info dbmr = DefGodMchWapper.selectMchInfo(agentid).get(0);
//			Logger.verifyEquals(bean.getAvatar(), dbmr.getAvatar(), "验证Avatar");
//			Logger.verifyEquals(bean.getMchTitle(), dbmr.getMchTitle(), "验证MchTitle");
//			Logger.verifyEquals(bean.getWechat(), dbmr.getWechat(), "验证Wechat");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_editmch_005() {
		Logger.start(true,"设置商户信息(头像,名称,微信号),验证微信号含有有emoji,,返回成功");
		try {
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			String code = GodmchManageCallService.createCodeCall(ccbean).getCode();
			Integer agentid = GodmchManageCallService.CreateSellerMch(code);
			bean.setAvatar("http://www.ymatou.com/new.jpg");
			bean.setMchId(agentid);
			bean.setMchTitle("updateTitle");
			bean.setWechat("uwechat😀");
			call.setData(bean);
			call.callService();			
			Logger.verifyEquals("200", call.getString("status"), "验证status");
			 mch_info dbmr = DefGodMchWapper.selectMchInfo(agentid).get(0);
			Logger.verifyEquals(bean.getAvatar(), dbmr.getAvatar(), "验证Avatar");
			Logger.verifyEquals(bean.getMchTitle(), dbmr.getMchTitle(), "验证MchTitle");
			Logger.verifyEquals(bean.getWechat(), dbmr.getWechat(), "验证Wechat");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_editmch_006() {
		Logger.start(true,"设置商户信息(头像,名称,微信号),验证商户id为空,返回失败,提示错误");
		try {
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			String code = GodmchManageCallService.createCodeCall(ccbean).getCode();
			Integer agentid = GodmchManageCallService.CreateSellerMch(code);
			bean.setAvatar("http://www.ymatou.com/new.jpg");
//			bean.setMchId(agentid);
			bean.setMchTitle("updateTitle");
			bean.setWechat("uwechat");
			call.setData(bean);
			call.callService();			
			Logger.verifyEquals("4001", call.getString("status"), "验证status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_editmch_007() {
		Logger.start(true,"设置商户信息(头像,名称,微信号),验证店铺名称、店铺logo、微信号不能同时为空");
		try {
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			String code = GodmchManageCallService.createCodeCall(ccbean).getCode();
			Integer agentid = GodmchManageCallService.CreateSellerMch(code);
//			bean.setAvatar("http://www.ymatou.com/new.jpg");
			bean.setMchId(agentid);
//			bean.setMchTitle("updateTitle");
//			bean.setWechat("uwechat");
			call.setData(bean);
			call.callService();			
			Logger.verifyEquals("5020", call.getString("status"), "验证status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_editmch_008() {
		Logger.start(true,"设置商户信息不输入头像,不发送修改头像总线消息");
		try {
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			String code = GodmchManageCallService.createCodeCall(ccbean).getCode();
			Integer agentid = GodmchManageCallService.CreateSellerMch(code);
//			bean.setAvatar("http://www.ymatou.com/new.jpg");
			bean.setMchId(agentid);
			bean.setMchTitle("updateTitle");
			bean.setWechat("uwechat");
			call.setData(bean);
			call.callService();			
			Logger.verifyEquals("200", call.getString("status"), "验证status");
			 mch_info dbmr = DefGodMchWapper.selectMchInfo(agentid).get(0);
			Logger.verifyEquals(DGUserCallService.SellerDefImg, dbmr.getAvatar(), "验证Avatar");
			Logger.verifyEquals(bean.getMchTitle(), dbmr.getMchTitle(), "验证MchTitle");
			Logger.verifyEquals(bean.getWechat(), dbmr.getWechat(), "验证Wechat");
			MessageListBean MessageListBean = MqQueryCallService.initGodmchManage("change_avatar-"+agentid,"change_avatar");
			List <Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
			if(joa.size()==0){
				Thread.sleep(5000);
				 joa = MqQueryCallService.GetMqMessage(MessageListBean);
			}
			joa.forEach(ele->{
				Logger.debug(ele.toString());
			});
			Logger.verifyEquals(true, joa.isEmpty(), "验证不修改时候不发送总线消息mchId");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_editmch_008_1() {
		Logger.start(true,"设置商户信息不输入头像,不发送修改头像总线消息");
		try {
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			String code = GodmchManageCallService.createCodeCall(ccbean).getCode();
			Integer agentid = GodmchManageCallService.CreateSellerMch(code);
			//bean.setAvatar("http://www.ymatou.com/new.jpg");
			bean.setAvatar(DGUserCallService.SellerDefImg);
			bean.setMchId(agentid);
			bean.setMchTitle("updateTitle");
			bean.setWechat("uwechat");
			call.setData(bean);
			call.callService();			
			Logger.verifyEquals("200", call.getString("status"), "验证status");
			mch_info dbmr = DefGodMchWapper.selectMchInfo(agentid).get(0);
			Logger.verifyEquals(bean.getAvatar(), dbmr.getAvatar(), "验证Avatar");
			// Logger.verifyEquals(DGUserCallService.DefImg2, dbmr.getAvatar(), "验证Avatar");
			Logger.verifyEquals(bean.getMchTitle(), dbmr.getMchTitle(), "验证MchTitle");
			Logger.verifyEquals(bean.getWechat(), dbmr.getWechat(), "验证Wechat");
			MessageListBean MessageListBean = MqQueryCallService.initGodmchManage("change_avatar-"+agentid,"change_avatar");
			List <Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
			if(joa.size()==0){
				Thread.sleep(5000);
				 joa = MqQueryCallService.GetMqMessage(MessageListBean);
			}
			joa.forEach(ele->{
				Logger.debug(ele.toString());
			});
			Logger.verifyEquals(true, joa.isEmpty(), "验证不修改时候不发送总线消息mchId");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_editmch_009() {
		Logger.start(true,"mchTitle 字段禁止使用emoji表情");
		try {
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			String code = GodmchManageCallService.createCodeCall(ccbean).getCode();
			Integer agentid = GodmchManageCallService.CreateSellerMch(code);
			//bean.setAvatar("http://www.ymatou.com/new.jpg");
			bean.setAvatar(DGUserCallService.SellerDefImg);
			bean.setMchId(agentid);
			bean.setMchTitle("😃");
			bean.setWechat("uwechat");
			call.setData(bean);
			call.callService();	
			
			Logger.verifyEquals("5000", call.getString("status"), "验证status");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}